package com.hrms.service.impl;

import com.hrms.dao.AttenManageMapper;
import com.hrms.dao.EmpInformationMapper;
import com.hrms.dao.SalaryInfoMapper;
import com.hrms.dao.SysDeptMapper;
import com.hrms.pojo.AttendInfo;
import com.hrms.pojo.EchartsData;
import com.hrms.service.EchartsDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @AUTHOR LK
 * @CREATE 2021-10-25-9:59
 */
@Service
public class EchartsDataServiceImpl implements EchartsDataService {

	@Autowired(required = false)
	AttenManageMapper attenManageMapper;

	@Autowired(required = false)
	SalaryInfoMapper salaryInfoMapper;

	@Autowired(required = false)
	EmpInformationMapper empInformationMapper;

	@Autowired(required = false)
	SysDeptMapper sysDeptMapper;

	@Override
	public HashMap<String, Object> queryAttendByMonth() {
		HashMap<String, Object> map = new HashMap<>();

		List<AttendInfo> results = new LinkedList<>();
		for (int i = 1; i < 13; i++) {
			AttendInfo attendInfo = new AttendInfo();
			attendInfo.setMonth(String.valueOf(i));
			attendInfo.setLateCounts(attenManageMapper.queryLateByMonth(i));
			attendInfo.setLeaveCounts(attenManageMapper.queryLeaveByMonth(i));
			results.add(attendInfo);
		}
		if (results.size() > 0) {
			map.put("info", "查询成功");
			map.put("list", results);
		} else {
			map.put("info", "查询失败");
		}
		return map;
	}

	@Override
	public HashMap<String, Object> queryDeptBySalary() {
		HashMap<String, Object> map = new HashMap<>();
		//求出总额
		List<Integer> leader = salaryInfoMapper.queryDeptBySalary("董事会");
		List<Integer> market = salaryInfoMapper.queryDeptBySalary("市场部");
		List<Integer> shop = salaryInfoMapper.queryDeptBySalary("销售部");
		List<Integer> salary = salaryInfoMapper.queryDeptBySalary("财务部");
		List<Integer> personal = salaryInfoMapper.queryDeptBySalary("人事部");

		List<EchartsData> data = new ArrayList<>();
		data.add(getEchartsData("董事会", leader));
		data.add(getEchartsData("市场部", market));
		data.add(getEchartsData("销售部", shop));
		data.add(getEchartsData("财务部", salary));
		data.add(getEchartsData("董事会", leader));
		data.add(getEchartsData("人事部", personal));

		if (data.size() > 0) {
			map.put("info", "查询成功");
			map.put("data", data);
		} else {
			map.put("info", "查询失败");
		}


		return map;
	}

	@Override
	public HashMap<String, Object> querySex() {
		HashMap<String, Object> map = new HashMap<>();
		List<EchartsData> result = new ArrayList<>();
		EchartsData data = new EchartsData();
		//男女比例
		Integer man = empInformationMapper.querySexCount("男");
		Integer woman = empInformationMapper.querySexCount("女");
		result.add(setEchartsData("男",man));
		result.add(setEchartsData("女",woman));

		map.put("data", result);

		return map;
	}

	@Override
	public HashMap<String, Object> queryAge() {
		HashMap<String, Object> map = new HashMap<>();
		List<EchartsData> result = new ArrayList<>();
		EchartsData data = new EchartsData();

		//年龄
		List<Integer> birthList = empInformationMapper.queryAgeCount();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		String nowYear = sdf.format(new Date());
		System.out.println(nowYear);
		HashSet<String> set = new HashSet<>();
		List<String> ages = new ArrayList<>();


		for (Integer birth : birthList) {
			Integer age = Integer.parseInt(nowYear) - birth;
			System.out.println(age);
			set.add(String.valueOf(age));
			ages.add(String.valueOf(age));
		}

		for (int i = 0; i < ages.size(); i++) {

			int counts = frequency(ages, ages.get(i));

			System.out.println(setEchartsData(ages.get(i),counts));
			if (!result.contains(setEchartsData(ages.get(i), counts))) {
				result.add(setEchartsData(ages.get(i),counts));
			} else if (result.contains(setEchartsData(ages.get(i), counts))) {
				result.set(result.indexOf(setEchartsData(ages.get(i), counts)), setEchartsData(ages.get(i), counts ));
			}

		}


		map.put("data", result);

		return map;
	}

	@Override
	public HashMap<String, Object> queryStu() {
		HashMap<String, Object> map = new HashMap<>();
		List<EchartsData> result = new ArrayList<>();
		EchartsData data = new EchartsData();


		//学历比例
		Integer zhuanke = empInformationMapper.queryStuCount("专科");
		Integer benke = empInformationMapper.queryStuCount("本科");
		Integer shuoshi = empInformationMapper.queryStuCount("硕士");
		Integer boshi = empInformationMapper.queryStuCount("博士");

		result.add(setEchartsData("专科",zhuanke));
		result.add(setEchartsData("本科",benke));
		result.add(setEchartsData("硕士",shuoshi));
		result.add(setEchartsData("博士",boshi));


		map.put("data", result);

		return map;
	}

	@Override
	public HashMap<String, Object> queryTopSalaryMonth() {
		HashMap<String, Object> map = new HashMap<>();

		for (int i = 1; i <= 12; i++) {
			List<EchartsData> items = salaryInfoMapper.queryTopSalaryMonth(i);
			map.put(i + "月", items);
		}

		return map;
	}

	@Override
	public HashMap<String, Object> queryDept() {
		HashMap<String, Object> map = new HashMap<>();
		List<EchartsData> results = new ArrayList<>();
		Integer leader = sysDeptMapper.queryCount("董事会");
		Integer market = sysDeptMapper.queryCount("市场部");
		Integer shop = sysDeptMapper.queryCount("销售部");
		Integer money = sysDeptMapper.queryCount("财务部");
		Integer personal = sysDeptMapper.queryCount("人事部");

		results.add(setEchartsData("董事会", leader));
		results.add(setEchartsData("市场部", market));
		results.add(setEchartsData("销售部", shop));
		results.add(setEchartsData("财务部", money));
		results.add(setEchartsData("人事部", personal));

		map.put("data", results);
		return map;
	}

	private static EchartsData setEchartsData(String name, Integer value) {
		EchartsData data = new EchartsData();
		data.setName(name);
		data.setValue(String.valueOf(value));
		return data;
	}

	private EchartsData getEchartsData(String dept, List<Integer> data) {
		//创建一个空数组

		EchartsData em = new EchartsData();
		int sum = 0;
		for (Integer temp : data) {
			sum += temp;
		}
		em.setName(dept);
		if (data.size() != 0) {
			em.setValue(String.valueOf(sum / data.size()));
		} else {
			em.setValue("0");
		}

		return em;
	}


	private static int frequency(List<String> list, String string) {
		int count = 0;

		for (String s : list) {
			if (string.equals(s)) {
				count++;
			}

		}
		return count;


	}


}
